//
//  CJSCitiesViewController.m
//  GoTraveling
//
//  Created by 陈金山 on 15/12/25.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "CJSCitiesViewController.h"
#import "Resources.h"
#import "CJSCitiesCell.h"
#import "CJSSectionHeadView.h"
#import "CJSSectionFootView.h"

@interface CJSCitiesViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) NSInteger i;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *butArray;

@property (nonatomic, retain) NSMutableArray *citiesArray;

@property (nonatomic, retain) UIView *cellView;

@end

@implementation CJSCitiesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.i = 1000;
    self.butArray = [NSMutableArray array];
    
    for (int i = 0; i < 5; i++) {
        [self.butArray addObject:@"1"];
    }
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[CJSCitiesCell class] forCellReuseIdentifier:@"identify"];
    
    [self AFNWorking];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    //    return self.citiesArray.count;
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    CJSSectionHeadView *headerView = [[CJSSectionHeadView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(SCREEN_WIDTH - 70, 0, 50, 40);
    button.tag = 1000 + section;
    button.backgroundColor = [UIColor colorWithWhite:0.937 alpha:1.000];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:button];
    
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    CJSSectionFootView *footerView = [[CJSSectionFootView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    return footerView;
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([[_butArray objectAtIndex:section] isEqualToString:@"0"]) {
        return 0;
    }
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CJSCitiesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identify"];
    
    self.cellView = cell.contentView;
    
    return cell;
}

#pragma mark - 网络请求
- (void)AFNWorking {
    
    NSString *strUrl = [NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/schedule/", self.spot_id];
    
    [Resources getDataWithName:strUrl block:^(id result) {
        
        NSDictionary *dic = result;
        
        
    } error:^{
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络连接错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
    }];
}

- (void)buttonAction:(UIButton *)button{
    
    
    if ([[_butArray objectAtIndex:button.tag - 1000]  isEqualToString:@"0"]) {
        [self.butArray replaceObjectAtIndex:button.tag - 1000 withObject: @"1"];
        self.i = 1000;
        [self.tableView reloadData];
    }
    else {
        [_butArray replaceObjectAtIndex:button.tag - 1000 withObject:@"0"];
        self.i = 1000;
        [UIView animateWithDuration:3 animations:^{
            
            self.cellView.hidden = YES;
            [self.tableView reloadData];
        }];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
